import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from util_proxy_ip_pool import API

'''Selenium基本使用'''
class UseSelenium():
    def __init__(self):
        # 获取代理IP
        ip_info_list = API().__query_list__()
        self.proxies = []
        for ip_info in ip_info_list:
            self.proxies.append(ip_info[0] + '://' + ip_info[1] + ':' + ip_info[2])


    def __get_cookies__(self):
        '''获取网站cookie'''
        driver = webdriver.Chrome()
        driver.get(url='https://www.baidu.com/')
        driver.implicitly_wait(1)
        print(driver.get_cookies())
        driver.quit()
    def __search_python__(self):
        '''搜索关键字python'''
        driver = webdriver.Chrome()
        driver.get(url='https://www.baidu.com/')
        driver.implicitly_wait(1)
        # 在百度搜索框中搜索'python'
        driver.find_element(by=By.ID, value='kw').send_keys('python')
        # 点击'百度搜索'
        driver.find_element(by=By.ID, value='su').click()
        time.sleep(5)
        driver.quit()
    def __toggle_label__(self):
        '''切换标签页，截屏'''
        driver = webdriver.Chrome()
        driver.get(url='https://www.baidu.com/')
        driver.implicitly_wait(1)
        # 打开新标签
        open_new_page_js = "window.open('https://juejin.cn/')"
        driver.execute_script(open_new_page_js)
        time.sleep(3)
        # 获取当前所有的标签页的句柄构成的列表
        current_windows = driver.window_handles
        # 根据标签页句柄列表索引下标进行切换
        driver.switch_to.window(current_windows[0])
        time.sleep(3)
        driver.save_screenshot('./images/screen.png')
        driver.quit()

    def __search_and_auto_open_result__(self):
        '''搜索并自动访问第一条查询结果'''
        driver = webdriver.Chrome()
        driver.get(url='https://www.baidu.com/')
        driver.implicitly_wait(1)
        # 在百度搜索框中搜索'python'
        driver.find_element(by=By.ID, value='kw').send_keys('python')
        # 点击'百度搜索'
        driver.find_element(by=By.ID, value='su').click()
        time.sleep(3)
        result_list = driver.find_elements(by=By.CLASS_NAME, value='c-title')
        # 点击第一条查询结果
        result_list[1].find_element(by=By.TAG_NAME, value='a').click()
        time.sleep(8)
        # 获取当前所有的标签页的句柄构成的列表
        current_windows = driver.window_handles
        # 根据标签页句柄列表索引下标进行切换
        driver.switch_to.window(current_windows[1])
        time.sleep(3)
        driver.save_screenshot('./images/screen.png')
        driver.quit()

    def __proxy_ip__(self):
        '''通过代理IP访问网站'''
        # 创建一个配置对象
        chrome_options = Options()
        # 使用代理ip
        proxy_server = random.choice(self.proxies)
        print(proxy_server)
        chrome_options.add_argument('--proxy-server=' + proxy_server)
        driver = webdriver.Chrome(options=chrome_options)
        driver.get('https://juejin.cn/')
        driver.implicitly_wait(3)
        print(driver.title)
        time.sleep(10)
        driver.quit()

if __name__ == '__main__':
    # API().__reload_ip_info__() # 重新载入代理IP数据
    obj = UseSelenium()
    obj.__proxy_ip__()
